#ifndef DEVICELIST_H
#define DEVICELIST_H
#include "indexguide.h"
#include "device.h"
#include <QAbstractListModel>
//#include <QVariantMap>
#include <QList>
#include <QString>
#include <QHash>
#include <QByteArray>
#include <QDateTime>
#include <QDateTime>
#include <QDebug>
namespace Moolink {


    class DeviceList : public QAbstractListModel
    {

        Q_OBJECT

    public:
    //    QString name;
    //    QString portname;
    //    QString protocol;
    //    int comaddr;
    //    QString colorCode;
        enum LineInfo_Roles{
            RoleName =Qt::UserRole + 1,
            RolePortname,
            RoleProtocolId,
            RoleProtocolLib,
            RoleProtocol,
            RoleCommaddr,
//            RoleHJcode,
//            RoleHJcodeName,
//            RoleHJunit,
//            RoleHJunitTotal,
            RolecolorCode,
            RoleSystemTime,
            RoledataString,
            RoleDeviceId
        };


    public:
        explicit DeviceList(QObject *parent = 0);

        int rowCount(const QModelIndex &parent) const;
        int rowCountRead();
        QVariant data(const QModelIndex &index, int role) const;
        QHash<int, QByteArray> roleNames() const;
        //void addDevice(QString name_, QString portname_, QString protocollib_, QString protocol_, QString addr_);
        void addDevice(QVariant devVar);
        void updateDevice(QVariant devVar);
        QList<Device> getdata(){return device_list;}
        int getdataSize(){return device_list.size();}
        int getIndexByDeviceId(QString deviceId);
        Device getdataByIndex(int index){return device_list[index];}
        Device* getpdataByIndex(int index){return &device_list[index];}
        void dataIndexNext(){if(deviceReadNextIndex<(device_list.size()-1)) deviceReadNextIndex++; else deviceReadNextIndex=0;}
        int getCurrDeviceCurrFuncode()
        {
            if(device_list[deviceReadNextIndex].dofunCurrIndex<device_list[deviceReadNextIndex].dofunlist.size())
            {
                deviceReadNextFunCode=device_list[deviceReadNextIndex].dofunlist.at(device_list[deviceReadNextIndex].dofunCurrIndex);
                return deviceReadNextFunCode;
            }
            else return 0;
        }

        void deviceOrFunIndexNext(){
            device_list[deviceReadNextIndex].dofunCurrIndex++;
            if(device_list[deviceReadNextIndex].dofunCurrIndex>=device_list[deviceReadNextIndex].dofunlist.size())
            {
                device_list[deviceReadNextIndex].dofunCurrIndex=0;
                dataIndexNext();
            }
        }
        void setdata(QList<Device>){}
        void changeDataString(int index,QString str,int error=0);
        Q_INVOKABLE QString getProtocolIdByDeviceId(int index){return device_list[index].protocolId;}
        Q_INVOKABLE void changeName(int index);
        Q_INVOKABLE void clickItem(int index);

        qint64 deviceReadNextSec=0;
        int deviceReadNextIndex=0;
        int deviceReadNextFunCode=0;

    signals:
        void dataChanged();
    public slots:

    private:
        QList<Device> device_list;
        QHash<int, QByteArray> m_roleNames;
    };

}


#endif // DEVICELIST_H
